package cn.myiotest.test3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Test1 {
    /*需求：
            一个文件里面存储了班级同学的信息，格式为：张三-男-23
            每一个学生信息占一行。
            要求通过程序实现随机点名器。
            70%的概率随机到男生
            30%的概率随机到女生
            随机100万次，统计结果。看生成男生和女生的比例是不是接近于7：3
        */
    public static void main(String[] args) {
        List<String> boyList = new ArrayList<>();
        List<String> girlList = new ArrayList<>();
        try(BufferedReader reader = new BufferedReader(new FileReader("move/names2.txt"))){
            String line;
            while((line=reader.readLine())!=null){
                String[] split = line.split("-");
                if(split[1].equals("男")){
                    boyList.add(split[0]);
                }else{
                    girlList.add(split[0]);
                }
            }
            // System.out.println(boyList);
            // System.out.println(girlList);
        }catch (Exception e){
            e.printStackTrace();
        }
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,1,1,1,1,1,1,0,0,0);
        Collections.shuffle(list);
        int boyCount = 0;
        int girlCount = 0;
        Random r = new Random();
        for (int i = 0; i < 10000; i++) {
            int index = r.nextInt(list.size());
            Integer weight = list.get(index);
            if(weight==1){
                System.out.println(boyList.get(index));
                boyCount++;
            }else{
                System.out.println(girlList.get(index));
                girlCount++;
            }
        }
        System.out.println("男生："+boyCount+" 女生："+girlCount);
        System.out.println("男生比例："+boyCount+"/"+(boyCount+girlCount)+"="+(boyCount/(boyCount+girlCount+0.0)*100)+"%");
        System.out.println("女生比例："+girlCount+"/"+(boyCount+girlCount)+"="+(girlCount/(boyCount+girlCount+0.0)*100)+"%");
    }
}
